/*
 * Created by: Peter Weissbrod
 * Created: Friday, November 09, 2007
 */
using System;
using EntityLayer.Entities;
using Utils;

namespace Controllers
{
    public class CourseController : ControllerBase<Course>
    {
        public CourseController(ICourseManagementView view) : base(view as IView)
        {
            view.CreateCourseEvent += new EventHandler<CourseEventArgs>(view_CreateCourseEvent);
            view.UpdateCourseEvent += new EventHandler<CourseEventArgs>(view_UpdateCourseEvent);
        }
        private void view_UpdateCourseEvent(object sender, CourseEventArgs e)
        {
            Course course = e.Course;
            Check.Assert(course.CourseID != Nulls.ID);
            ValidateData(course);
            course.UpdateAndFlush();
        }
        private void view_CreateCourseEvent(object sender, CourseEventArgs e)
        {
            Course course = e.Course;
            ValidateData(course);
            course.SaveAndFlush();
        }
        /// <summary>
        /// If the incoming data is invalid, an exception will be thrown here
        /// </summary>
        /// <param name="t"></param>
        protected override void ValidateData(Course t)
        {
            Check.Assert(t.CourseNameAndNumber.Length>0,"Name and number is unspecified");
            Check.Assert(
                Course.FindAllByProperty("CourseNameAndNumber", t.CourseNameAndNumber).Length == 0, "This course has already been defined"
                );
        }
    }
}